package com.shenmazong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.dto.TbSoftware;
import com.shenmazong.service.TbSoftwareService;
import com.shenmazong.mapper.TbSoftwareMapper;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.BatchChooseIdVo;
import com.shenmazong.vo.ListParamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 *
 */
@Service
public class TbSoftwareServiceImpl extends ServiceImpl<TbSoftwareMapper, TbSoftware>
implements TbSoftwareService{
    @Autowired
    private TbSoftwareMapper tbSoftwareMapper;

    @Override
    public ResultResponse findAllSoftwareAdmin(ListParamVo listParamVo) {
        Page<TbSoftware> page = tbSoftwareMapper.findAllSoftwareAdmin(new Page<>(listParamVo.getCurrent(), listParamVo.getSize())
                , listParamVo.getParamMap());
        return ResultResponse.SUCCESS(page);
    }

    @Override
    public ResultResponse finAllSoftwareStudent(ListParamVo listParamVo) {
        Page<TbSoftware> page = tbSoftwareMapper.finAllSoftwareStudent(new Page<>(listParamVo.getCurrent(), listParamVo.getSize())
                , listParamVo.getParamMap());
        return ResultResponse.SUCCESS(page);
    }

    @Override
    public ResultResponse saveSoftware(TbSoftware tbSoftware) {
        try {
            tbSoftwareMapper.insert(tbSoftware);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateSoftware(TbSoftware tbSoftware) {
        try {
            TbSoftware select = tbSoftwareMapper.selectById(tbSoftware.getSoftwareId());
            if (!StringUtils.isEmpty(tbSoftware.getSoftwareName())){
                select.setSoftwareName(tbSoftware.getSoftwareName());
            }
            if (!StringUtils.isEmpty(tbSoftware.getSoftwareWeb())){
                select.setSoftwareWeb(tbSoftware.getSoftwareWeb());
            }
            select.setUpdateTime(new Date());
            tbSoftwareMapper.updateById(select);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse deleteSoftware(BatchChooseIdVo batchChooseIdVo) {
        if (batchChooseIdVo.getIds().size()<=0){
            return ResultResponse.FAILED("至少选择一个哟");
        }
        try {
            for (Long id : batchChooseIdVo.getIds()) {
                TbSoftware byId = tbSoftwareMapper.selectById(id);
                byId.setDeleteFlag(true);
                tbSoftwareMapper.updateById(byId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse auditSoftware(BatchChooseIdVo batchChooseIdVo) {
        if (batchChooseIdVo.getIds().size()<=0){
            return ResultResponse.FAILED("至少选择一个哟");
        }
        try {
            Integer audit = (Integer) batchChooseIdVo.getParamMap().get("audit");
            for (Long id : batchChooseIdVo.getIds()) {
                TbSoftware byId = tbSoftwareMapper.selectById(id);
                byId.setSoftwareAudit(audit.byteValue());
                tbSoftwareMapper.updateById(byId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }
}




